package app;
import java.awt.*;
import java.awt.event.*;

public class CurrencyConverterRobotTester {
	// A robot object that will be used to exercise a GUI object.
	Robot robot;

	// The following GUI object will be exercised by the robot.
	 CurrencyConverterModel model = new CurrencyConverterModel();
	 CurrencyConverterView slave = new CurrencyConverterView(model);
	 CurrencyConverterController controller = new CurrencyConverterController(slave, model);
	

	// The following text will be entered into the text field 
	// on the GUI.
	int key100[] = {
			KeyEvent.VK_1,
			KeyEvent.VK_0,
			KeyEvent.VK_0,
			KeyEvent.VK_DECIMAL,
			KeyEvent.VK_5,
			KeyEvent.VK_0
	};

	public CurrencyConverterRobotTester() {
		try{
			slave.setVisible(true);
			robot = new Robot();
			
		}catch(AWTException e){
			e.printStackTrace();
		}

		// Conversion from US Dollar to Brazilian Reals
		convertUsDollarToBrazilianReals();

		// Conversion from US Dollar to Canadian Dollars		
		convertUsDollarToCanadianDollars();

		// Conversion from US Dollar to Euro Dollars
		convertUsDollarToEuroDollars();

		// Conversion from US Dollar to Japanese Yen
		convertUsDollarToJapaneseYen();

		// Exit Currency Converter Application
		exitCurrencyConverterApplication();
		
	}

	public void mouseMoveAndClick(int xLoc, int yLoc){
		robot.mouseMove( xLoc,yLoc );
		robot.delay(1000);
		robot.mousePress(InputEvent.BUTTON1_MASK);
		robot.delay(1000);
		robot.mouseRelease(InputEvent.BUTTON1_MASK);
		robot.delay(1000);
	}

	public void convertUsDollarToBrazilianReals(){
		robot.delay(1000); 
		slave.getTxtFldUSDollarAmount().requestFocus();
		for (int i = 0; i < key100.length; i++) {
			robot.keyPress(key100[i]);
			robot.delay(500);
		}

		// move cursor to radio button Brazil
		Point rbp = slave.getJrbtnBrazil().getLocationOnScreen();
		mouseMoveAndClick(rbp.x, rbp.y);

		// move cursor to compute button
		Point cpb = slave.getJbtnCompute().getLocationOnScreen();
		mouseMoveAndClick(cpb.x, cpb.y);

		// move the cursor to clear button
		// move cursor to compute button
		Point clrb = slave.getJbtnClear().getLocationOnScreen();
		mouseMoveAndClick(clrb.x, clrb.y);

	}

	public void convertUsDollarToCanadianDollars(){
		robot.delay(1000); 
		slave.getTxtFldUSDollarAmount().requestFocus();
		for (int i = 0; i < key100.length; i++) {
			robot.keyPress(key100[i]);
			robot.delay(500);
		}

		// move cursor to radio button Brazil
		Point rbp = slave.getJrbtnCanada().getLocationOnScreen();
		mouseMoveAndClick(rbp.x, rbp.y);

		// move cursor to compute button
		Point cpb = slave.getJbtnCompute().getLocationOnScreen();
		mouseMoveAndClick(cpb.x, cpb.y);

		// move the cursor to clear button
		// move cursor to compute button
		Point clrb = slave.getJbtnClear().getLocationOnScreen();
		mouseMoveAndClick(clrb.x, clrb.y);
	}

	public void convertUsDollarToEuroDollars(){
		robot.delay(1000); 
		slave.getTxtFldUSDollarAmount().requestFocus();
		for (int i = 0; i < key100.length; i++) {
			robot.keyPress(key100[i]);
			robot.delay(500);
		}

		// move cursor to radio button Brazil
		Point rbp = slave.getJrbtnEU().getLocationOnScreen();
		mouseMoveAndClick(rbp.x, rbp.y);

		// move cursor to compute button
		Point cpb = slave.getJbtnCompute().getLocationOnScreen();
		mouseMoveAndClick(cpb.x, cpb.y);

		// move the cursor to clear button
		// move cursor to compute button
		Point clrb = slave.getJbtnClear().getLocationOnScreen();
		mouseMoveAndClick(clrb.x, clrb.y);
	}	

	public void convertUsDollarToJapaneseYen(){
		robot.delay(1000); 
		slave.getTxtFldUSDollarAmount().requestFocus();
		for (int i = 0; i < key100.length; i++) {
			robot.keyPress(key100[i]);
			robot.delay(500);
		}

		// move cursor to radio button Brazil
		Point rbp = slave.getJrbtnJapan().getLocationOnScreen();
		mouseMoveAndClick(rbp.x, rbp.y);

		// move cursor to compute button
		Point cpb = slave.getJbtnCompute().getLocationOnScreen();
		mouseMoveAndClick(cpb.x, cpb.y);

		// move the cursor to clear button
		Point clrb = slave.getJbtnClear().getLocationOnScreen();
		mouseMoveAndClick(clrb.x, clrb.y);
	}		

	public void exitCurrencyConverterApplication(){
		robot.delay(2000); 
		// move the cursor to exit button
		Point clrb = slave.getJbtnQuit().getLocationOnScreen();
		mouseMoveAndClick(clrb.x, clrb.y);
	}				
    // Launch the Robot Tester
	public static void main(String args[]){
		System.out.println("Start test program");
		new CurrencyConverterRobotTester();
	}
}